package com.mk.api.sys;

import com.mk.base.model.response.Rsp;
import com.mk.model.request.RoleRequest;
import com.mk.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: yq
 * @Description:
 * @Date: Created in 1:58 PM 3/14/18
 */
@RestController
@RequestMapping("/role")
public class RoleApi {

    @Autowired
    private IRoleService roleService;

    @RequestMapping(method = RequestMethod.POST)
    public Rsp saveRole(@RequestBody RoleRequest request){
        try{
            return Rsp.SUCCESS(roleService.saveRole(request));
        }catch (Exception e){
            return Rsp.ERROR(e.getMessage());
        }
    }

    @RequestMapping(method = RequestMethod.PUT)
    public Rsp updateRole(@RequestBody RoleRequest request){
        try{
            if(roleService.updateRole(request))
                return Rsp.SUCCESS("编辑角色信息成功");
            else
                return Rsp.ERROR("编辑角色信息失败");
        }catch (Exception e){
            return Rsp.ERROR(e.getMessage());
        }
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Rsp deleteRole(@PathVariable("id") Long id){
        try{
            if(roleService.deleteRole(id))
                return Rsp.SUCCESS("删除角色信息成功");
            else
                return Rsp.ERROR("删除角色信息失败");
        }catch (Exception e){
            return Rsp.ERROR(e.getMessage());
        }
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Rsp findOne(@PathVariable("id") Long id){
        return Rsp.SUCCESS(roleService.get(id));
    }

    @RequestMapping(value = "/username",method = RequestMethod.GET)
    public Rsp findByUsername(@RequestParam("username") String username){
        return Rsp.SUCCESS(roleService.getByUsername(username));
    }

    @RequestMapping(method = RequestMethod.GET)
    public Rsp findAll(){
        return Rsp.SUCCESS(roleService.findAll());
    }
}
